


$(document).ready(function() {
	
	var tableContainer = $('#tableContainer');
	
	// localization
	var messages = {
		serverCommunicationError: 'Ocorreu um erro na comunicação com o servidor.',
	    loadingMessage: 'A carregar ...',
	    noDataAvailable: 'Sem dados!',
	    addNewRecord: '+ Adicionar Utilizador',
	    editRecord: 'Editar Registo',
	    areYouSure: 'Tem a certeza?',
	    deleteConfirmation: 'Este registo será eliminado. Tem a certeza?',
	    save: 'Guardar',
	    saving: 'A guardar',
	    cancel: 'Cancelar',
	    deleteText: 'Eliminar',
	    deleting: 'A eliminiar',
	    error: 'Erro',
	    close: 'Fechar',
	    cannotLoadOptionsFor: 'Não foi possivel carregar as opções do campo {0}',
	    pagingInfo: 'Mostrando {0} até {1} de {2} registos',
	    canNotDeletedRecords: 'Não foi possivel eliminar {0} de {1} registos!',
	    deleteProggress: 'Eliminado {0} de {1} registos, processando...'
	};
	
	tableContainer.jtable({
	
		// Field Options
		title:					'Utilizadores',
		paging:					false,
		sorting:				false,
		deleteConfirmation:		true,
		dialogShowEffect:		'fade',	
	    dialogHideEffect:		'fade',
	    selectOnRowClick:		false,
	    selectingCheckboxes:	false,
	    selecting:				false,
	    multiselect:			false,
	    saveUserPreferences:	false,
	    messages: 				messages,
	
		// Actions
		actions: {
	        createAction: 	window.ajaxUrl + '/jtableCreateUser',
	        listAction: 	window.ajaxUrl + '/jtableReadUser',
	        updateAction: 	window.ajaxUrl + '/jtableUpdateUser',
	        deleteAction: 	window.ajaxUrl + '/jtableDeleteUser'
		},
	
		// Show Fields
		fields: {
			id: {
				key:			true,
				edit:			false,
				create:			false,
				list:			false
			},
			username: {
				title:			'username',
				width:			'4%',
				type:			'text',
				edit:			false,
	            create:			false,
	            list:			false,
	            inputClass: 	'validate[required]'
			},
			company: {
				title:			'Nome',
				width:			'4%',
				type:			'text',
				edit:			false,
	            create:			false,
	            list:			true,
	            inputClass: 	'validate[required]'
			},
			groupName: {
				title:			'Tipo de conta',
				width:			'5%',
				//type:			'text',
				options:		{'school':'Escola','hotel':'Hotel'},
				edit:			false,
	            create:			true,
	            list:			true,
	            //inputClass: 	'validate[required,custom[number]]'
			},
			active: {
				title:			'Activo',
				width:			'4%',
	            type:			'checkbox',
	            values:			{'0':'não','1':'sim'},
	            setOnTextClick:	true,
	            defaultValue:	'1',
				edit:			true,
				create:			true,
				list:			true
			},
			email: {
				title:			'Mail',
				width:			'10%',
	            type:			'text',
				edit:			true,
	            create:			true,
	            list:			true,
			},
			/*
			group: {
				title:			'tipo de conta',
				width:			'10%',
				options:		{'school':'Escola','hotel':'Hotel'},
				edit:			true,
	            create:			true,
	            list:			false
			},
			*/
			password: {
				title: 			'Password',
				type:			'password',
				edit:			true,
	            create:			true,
	            list:			false
			},
			retypePassword: {
				title: 			'Repetir Password',
				type:			'password',
				edit:			true,
	            create:			true,
	            list:			false
			},
			phone: {
				title:			'Telefone',
				width:			'6%',
	            type:			'text',
				edit:			false,
	            create:			false,
	            list:			true
			},
			address: {
				title:			'Morada',
				width:			'20%',
	            type:			'textarea',
				edit:			false,
	            create:			false,
	            list:			true
			}
		},
		
		// INI validation Logic
		formCreated: function(event,data) {
			data.form.validationEngine();
		},
		
		// Validate
		formSubmitting: function(event, data) {
			return data.form.validationEngine('validate');
		},
		
		// Dispose validation logic when form is closed
        formClosed: function (event, data) {
            data.form.validationEngine('hide');
            data.form.validationEngine('detach');
        }
	});
	
	//call readOfferAction and load data from server into the table
	tableContainer.jtable('load');
});
